package com.lzy.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzy 线程字段工具类
 */
@Slf4j
public class ThreadLocalUtil {
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();

    public ThreadLocalUtil() {
    }

    public static void setObject(Map<String, Object> map) {
        Map<String, Object> objectMap = THREAD_LOCAL.get();
        if (CollectionUtils.isEmpty(objectMap)) {
            objectMap = new HashMap<>();
        }
        objectMap.putAll(map);
        THREAD_LOCAL.set(objectMap);
    }

    public static Map<String, Object> getObject() {
        return THREAD_LOCAL.get();
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }
}